﻿using VM.Common.Enums;
using VM.Dialogs.Views;
using VM.Start.Dialogs.Views;
using VM.Start.Services;
using VM.Start.Views.Dock;

namespace VM.Start.Methods
{
    public class SolutionOperator
    {
        /// <summary>
        /// 打开解决方案
        /// </summary>
        /// <param name="solutionPath"></param>
        /// <returns></returns>
        public static bool OpenSolution(string? solutionPath = null)
        {
            if (solutionPath == null)
            {
                MessageView.Ins.MessageBoxShow("解决方案路径为空！", eMsgType.Error);
                return false;

            }
            else if (!System.IO.File.Exists(solutionPath))
            {
                MessageView.Ins.MessageBoxShow($"不存在该路径：{solutionPath}", eMsgType.Error);
                return false;
            }
            else
            {
                var view = LoadingView.Ins;
                view.LoadingShow("加载项目中，请稍等...");
                MainViewModel.Ins.OpenSolution(solutionPath);
                Task.Delay(8000);
                view.Close();
                return true;
            }
        }

        public static void SaveSolution()
        {
            MainViewModel.Ins.NavOperateCommand.Execute("Save");
        }

        public static void SaveAsSolution()
        {
            MainViewModel.Ins.NavOperateCommand.Execute("SaveAs"); ;
        }

        /// <summary>
        /// 获取解决方案的流程名
        /// </summary>
        /// <param name="solutionPath"></param>
        /// <returns></returns>
        public static List<string> GetProcessList()
        {
            List<string> ProcessList = new List<string>();
            foreach (var item in Solution.Ins.ProjectList)
            {
                var name = item.ProjectInfo.ProcessName;
                ProcessList.Add(name);
            }
            return ProcessList;
        }

        /// <summary>
        /// 选择流程
        /// </summary>
        /// <param name="processName"></param>
        /// <returns></returns>
        public static bool SelectProcess(string processName)
        {
            if (string.IsNullOrEmpty(processName))
            {
                MessageView.Ins.MessageBoxShow("流程名为空", eMsgType.Error);
                return false;
            }
            // 选择指定流程（如果提供）
            if (!ToolView.Ins.SelectProject(processName))
            {
                return false;
            }
            if (Solution.Ins.CurrentProject == null || Solution.Ins.CurrentProject.ModuleList.Count == 0)
            {
                return false;
            }
            Solution.Ins.CurrentProject.RunMode = eRunMode.RunOnce;
            Solution.Ins.CurrentProject.Start();
            return true;

        }

        /// <summary>
        /// 获取当前流程模块列表
        /// </summary>
        /// <returns></returns>
        public static List<string> GetModuleList()
        {
            List<string> ModuleList = new List<string>();

            foreach (var item in Solution.Ins.CurrentProject.ModuleList)
            {
                var name = item.ModuleParam.ModuleName;
                ModuleList.Add(name);
            }
            return ModuleList;
        }


        public static bool RunOnceProcess(string? processName = null)
        {
            // 选择指定流程（如果提供）
            if (!string.IsNullOrEmpty(processName))
            {
                if (!ToolView.Ins.SelectProject(processName))
                {
                    return false;
                }
            }
            return Solution.Ins.CurrentProject.ExcuteMultiModuleByName();
        }

        public static bool RunOnceProcess(int processID = 0)
        {
            // 选择指定流程（如果提供）
            if (!ToolView.Ins.SelectProject(processID))
            {
                return false;
            }

            return Solution.Ins.CurrentProject.ExcuteMultiModuleByName();
        }

    }
}
